package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.FileSubufferUtils;
import com.sumainfor.common.utlis.HtmlUtil;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.KeepRepairMapper;
import com.sumainfor.pc.epc.dao.UtilsMapper;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述:
 * 管家维修工单
 * @Auther: zhlu
 * @Date: 2019/3/5 16:00
 * @Description:
 */
@Service
public class KeepRepairService {
    private ReentrantLock lock = new ReentrantLock(false);

    @Autowired
    UtilsMapper utilsMapper;

    String UUID=null;

    @Autowired
    KeepRepairMapper keepRepairMapper;

    @Autowired
    private ZzuulFeign zzuulFeign;


    /**
     * 功能描述:
     * 获取维修工单列表
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/5 16:02
     */
    public List<Map<String,Object>>getRepAirList(Map<String,Object>params){
        List<Map<String,Object>>getRepAirList=keepRepairMapper.getRepAirList(params);
        return getRepAirList;
    }

    /**
     * 功能描述: 
     * 获取维修工单列表总数
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/3/5 16:03
     */
    public Integer getRepAirListCount(Map<String,Object>params){
        Integer getRepAirListCount=keepRepairMapper.getRepAirListCount(params);
        return getRepAirListCount;
    }

    /**
     * 功能描述: 
     * 管家发起维修申请
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/4/26 11:46
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean AddRepair(Map<String,Object>params){
        boolean result=false;
        lock.lock();
        try {
            try {
                Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                    put("uuid", "RepId");
                    put("database", Constants.DATA_SOURCE_TCEPC);
                    put("surface", "tcepc_repair");
                }});
                if(uuid!=null){
                    int number=Integer.valueOf(uuid.get("uuid").toString());
                    UUID= ComUtils.getLocalTrmSeqNum("wxdg",number);
                }else{
                    UUID=ComUtils.getLocalTrmSeqNum("wxdg",0);
                }
                //所有新增处理
                Map<String,Object>commonInsertParms=new HashMap<String,Object>();
                commonInsertParms.put("by", ContextUtils.getUserId());//操作人
                commonInsertParms.put("RepairsId", ContextUtils.getUserId());//报修人编号
                commonInsertParms.put("RepId",UUID);//维修工单编号
                commonInsertParms.put("term", "4");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                commonInsertParms.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonInsertParms(commonInsertParms);
                params.putAll(commonInsertParms);

                //管家设备维修照片
                List<Map<String, Object>> TroubleFileList = (List<Map<String, Object>>) params.get("TroubleFile");
                String TroubleFile = FileSubufferUtils.ListFlieStringUtils(TroubleFileList);
                params.put("TroubleFile",TroubleFile);//图片

                //发起维修申请
                keepRepairMapper.AddRepair(params);
                result=true;

                //新维修单
                CustomMap data = CustomMap.create("MeType", 8)
                        .put("Title", "新维修工单")
                        .put("Content", HtmlUtil.stringToHTML("管家发起新的维修工单！；", "", Color.blue))
                        .put("by", ContextUtils.getUserId())
                        .put("term", 3);
                zzuulFeign.AddMessage(data);

            } catch (NumberFormatException e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }

}
